from fastapi import APIRouter, BackgroundTasks, Body, Depends, File, Query, UploadFile

from app.core.deps import CurrentUser
from app.schemas.admin.admin_file_manage_schemas import (
    FileDetailResponse,
    FileListFilter,
    FileMigrateRequest,
    FileStatusUpdateRequest,
)
from app.schemas.common import PaginationParams, PaginationResult
from app.schemas.response import Response
from app.services.admin.admin_file_manage_service import FileManageService

router = APIRouter(tags=["文件管理"])


@router.get("/list")
async def get_file_list(
    current_user: CurrentUser,
    pagination: PaginationParams,
    filters: FileListFilter,
) -> Response[PaginationResult[FileDetailResponse]]:
    """获取所有文件列表（支持高级筛选）"""
    service = FileManageService()
    result = await service.get_file_list(pagination, filters)
    return Response.success(data=result)


@router.get("/detail")
async def get_file_detail(
    current_user: CurrentUser,
    file_id: int = Query(..., description="文件ID"),
) -> Response[FileDetailResponse]:
    """获取文件详情"""
    service = FileManageService()
    result = await service.get_file_detail(file_id)
    return Response.success(data=result)


@router.post("/upload")
async def upload_file(
    current_user: CurrentUser,
    file: UploadFile = File(...),
    provider_id: int = Query(None, description="存储供应商ID"),
    background_tasks: BackgroundTasks = BackgroundTasks(),
) -> Response[FileDetailResponse]:
    """管理员上传文件（可指定存储供应商）"""
    service = FileManageService()
    result = await service.upload_file(file, current_user.id, background_tasks, provider_id)
    return Response.success(data=result)


@router.post("/delete")
async def delete_file(
    current_user: CurrentUser,
    file_id: int = Query(..., description="文件ID"),
) -> Response:
    """删除任意文件"""
    service = FileManageService()
    await service.delete_file(file_id)
    return Response.success()


@router.post("/batch-delete")
async def batch_delete_files(
    current_user: CurrentUser,
    file_ids: list[int] = Body(..., description="文件ID列表"),
) -> Response:
    """批量删除文件"""
    service = FileManageService()
    await service.batch_delete_files(file_ids)
    return Response.success()


@router.post("/migrate")
async def migrate_files(
    current_user: CurrentUser,
    in_data: FileMigrateRequest,
) -> Response:
    """文件迁移（跨存储供应商）"""
    service = FileManageService()
    await service.migrate_files(in_data)
    return Response.success()


@router.post("/status")
async def update_file_status(
    current_user: CurrentUser,
    in_data: FileStatusUpdateRequest,
) -> Response:
    """修改文件状态"""
    service = FileManageService()
    await service.update_file_status(in_data)
    return Response.success()
